import { Component } from '@angular/core';
import { NavController, NavParams, ViewController, LoadingController, ToastController } from 'ionic-angular';
import { RestProvider } from '../../providers/rest/rest';
import { BaseUI } from '../../common/baseui';
import { Storage } from '@ionic/storage';


/**
 * Generated class for the AnswerPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */


@Component({
  selector: 'page-answer',
  templateUrl: 'answer.html',
})
export class AnswerPage extends BaseUI {
  content: string;
  id: string;
  errMessage: any;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private viewCtrl: ViewController,
    private rest: RestProvider,
    private loadingCtrl: LoadingController,
    private storage: Storage,
    private toastCtrl: ToastController) {
    super();
    this.id = this.navParams.get('id');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad AnswerPage');
  }
  dismiss() {
    this.viewCtrl.dismiss();
  }
  sumbit() {
    this.storage.get('UserId').then((val) => {
      if (val != null) {
        var loading = super.showLoading(this.loadingCtrl, '发表中...');
        this.rest.answer(val, this.id, this.content)
          .subscribe(data => {
            if (data["Status"] == "OK") {
              loading.dismissAll();
              this.dismiss();
            } else {
              loading.dismissAll();
              this.dismiss();
              super.showToast(this.toastCtrl, data["StatusContent"]);
            }
          }, error => this.errMessage = <any>error);
      } else {
        super.showToast(this.toastCtrl, "请登陆后再回答。");
      }
    })
  }
}
